import Vue from 'vue'
import Vuex from 'vuex'
import $axios from 'axios'
import { Message } from 'element-ui'

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        menuList: [],
        menuInfo: {
            act: "getlist",
            start: 1,
            pagesize: 10,
        },
        tagList: [],
        tagInfo: {
            act: "getlist",
            start: 1,
            pagesize: 10,
        },
        tagTotal: 0,
        menuTotal: 0,
        advList: [],
        advInfo: {
            act: "getlist",
            start: 1,
            pagesize: 10,
        },
        advTotal: 0
    },
    mutations: {
        setMenuList(state, data) {
            state.menuList = data
        },
        setMenuInfo(state, data) {
            state.menuInfo = data
        },
        setMenuTotal(state, data) {
            state.menuTotal = data
        },
        setTagList(state, data) {
            state.tagList = data
        },
        setTagInfo(state, data) {
            state.tagInfo = data
        },
        setTagTotal(state, data) {
            state.tagTotal = data
        },
        setAdvTotal(state, data) {
            state.advTotal = data
        },
        setAdvInfo(state, data) {
            state.advInfo = data
        },
        setAdvList(state, data) {
            state.advList = data
        }
    },
    actions: {
        // 获取分类信息
        async getMenuList(context) {

            let { data: res } = await $axios.get("api/menu", {
                params: context.state.menuInfo,
            });
            if (res.code !== 200) return Message.error(res.msg);
            context.commit('setMenuList', res.data)
            context.commit('setMenuTotal', res.allNum)

        },
        // 获取标签信息
        async getTagList(context) {

            let { data: res } = await $axios.get("api/tag", {
                params: context.state.tagInfo,
            });
            if (res.code !== 200) return Message.error(res.msg);
            context.commit('setTagList', res.data)
            context.commit('setTagTotal', res.allNum)

        },
        // 获取广告位信息

        async getAdvList(context) {

            let { data: res } = await $axios.get("api/advpos", {
                params: context.state.advInfo,
            });
            if (res.code !== 200) return Message.error(res.msg);
            context.commit('setAdvList', res.data)
            context.commit('setAdvTotal', res.allNum)

        },
    }
})